// 检查数据库中的用户
const mysql = require('mysql2/promise');
require('dotenv').config({ path: '.env.local' });

async function checkUsers() {
  console.log('🔍 检查数据库中的用户...\n');

  const config = {
    host: process.env.DB_HOST || 'localhost',
    port: parseInt(process.env.DB_PORT || '3306'),
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || '',
    database: process.env.DB_NAME || 'math_academy',
  };

  try {
    const connection = await mysql.createConnection(config);
    console.log('✅ 数据库连接成功\n');

    // 查询所有用户
    const [users] = await connection.execute(
      'SELECT id, username, email, role, created_at, last_login_at FROM users ORDER BY id'
    );

    console.log(`📊 找到 ${users.length} 个用户:\n`);
    console.log('═══════════════════════════════════════════════════════════════');
    console.log('ID | 用户名 | 邮箱 | 角色 | 创建时间 | 最后登录');
    console.log('═══════════════════════════════════════════════════════════════');
    
    users.forEach(user => {
      const createdAt = new Date(user.created_at).toLocaleString('zh-CN');
      const lastLogin = user.last_login_at 
        ? new Date(user.last_login_at).toLocaleString('zh-CN')
        : '从未登录';
      
      console.log(
        `${user.id} | ${user.username} | ${user.email || '无'} | ${user.role} | ${createdAt} | ${lastLogin}`
      );
    });
    
    console.log('═══════════════════════════════════════════════════════════════\n');

    await connection.end();
  } catch (error) {
    console.error('❌ 错误:', error.message);
  }
}

checkUsers();
